<?php
namespace app\index\controller;

use think\Controller;
use think\Db;
use think\Request;
class Student extends Controller
{
	public function selectAll()
	{
		//$list = Db::query("select * from spr_student");
		$list=Db::name("student s")->field("*")
			->join('spr_class c','s.s_class=c.mr_id','LEFT')
			
			//->where('s_name','like','%李%')
			//->whereOr('s_class',0)
			->order('s_name DESC')
			->limit(2,4)
			//->fetchSql(true)
			->paginate(3,true);
		//var_dump($list);
		//die;
		$page = $list->render();
		
		return $this->fetch('selectAll',[
				'list'=>$list,
				'page'=>$page,
		]);
	}
	
	public function add()
	{
		//获取Request的实例
		$request = Request::instance();
		//如果点击新增的提交按钮，则执行
		if($request->has("sub")){
			$name = $request->post("s_name");
			$card = $request->post("s_card");
			$birthday = $request->param("s_birthday");
			$address = $request->param("s_address");
			//input("post.s_address");
			//input("param.s_address");
			$data=[ 's_name'=>$name,
					's_card'=>$card,
					's_birthday'=>$birthday,
					's_address'=>$address
			];
			$result = Db::table("spr_student")->insert($data);
			
			if($result){
				$this->success("新增成功","Student/selectAll");
			}else{
				$this->error("新增失败");
			}
		}else{
			return $this->fetch();
		}
	}
	
		public function update()
		{
			
			if(input("?post.sub")){//执行修改
				//
				$data=[
						"s_id" =>input("post.s_id"),
						"s_name"=>input("post.s_name"),
						"s_card"=>input("post.s_card"),
						"s_birthday"=>input("post.s_birthday"),
						"s_address"=>input("post.s_address"),
						"s_class"=>input("post.s_class"),
				];
				$result =Db::table("spr_student")->update($data);
			if($result){
				$this->success("修改成功","Student/selectAll");
			}else{
				$this->error("修改失败");
			}
		}else{
			//1.获取s_id值
			$s_id=input('param.s_id');
			//2.根据id查询该条数值  如果是根据主键查询，可以将值放到find()中
			$student = Db::table("spr_student")->where('s_id',$s_id)->find();
			//3.查询所有的班级
			$classList = Db::table("spr_class")->select();
			return $this->fetch('update',[
					'student'=>$student,
					'classList' =>$classList,
			]);
		}
	}
		
		public function del()
		{
				
		}
		
		
	
}
